<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-06-29 09:22
 * Modified Time: 2019-06-29 09:22
 * Version: 2.0
 */

namespace App\Modules\Order\Services;


use App\Exceptions\NotFoundException;
use App\Lib\Utilities;
use App\Modules\Order\Criterias\getCommentListCriteria;
use App\Modules\Order\Repositories\CommentResourceRepository;
use App\Modules\Order\Repositories\OrderCommentLikeRepository;
use App\Modules\Order\Repositories\OrderCommentReplyRepository;
use App\Modules\Order\Repositories\OrderCommentRepository;
use App\Modules\Order\Repositories\OrderRepository;
use App\Modules\System\Repositories\PictureRepository;
use App\Modules\System\Repositories\VideoRepository;
use Flower\Core\Repository\Eloquent\BaseService;
use Illuminate\Support\Facades\DB;

class OrderCommentService extends BaseService
{
	private $orderCommentRepository;
	private $orderCommentReplyRepository;
	private $orderCommentLikeRepository;
	private $commentResourceRepository;
	private $orderRepository;
	private $pictureRepository;
	private $videoRepository;
	public function __construct(
		OrderCommentRepository $orderCommentRepository,
		OrderCommentReplyRepository $orderCommentReplyRepository,
		OrderCommentLikeRepository $orderCommentLikeRepository,
	OrderRepository $orderRepository,CommentResourceRepository $commentResourceRepository,PictureRepository $pictureRepository,VideoRepository $videoRepository
	)
	{
		$this->orderRepository = $orderRepository;
		$this->orderCommentRepository = $orderCommentRepository;
		$this->orderCommentReplyRepository = $orderCommentReplyRepository;
		$this->orderCommentLikeRepository = $orderCommentLikeRepository;
		$this->commentResourceRepository =$commentResourceRepository;
		$this->pictureRepository = $pictureRepository;
		$this->videoRepository =$videoRepository;
	}

	/**
	 * 订单评论
	 * @param $memberId
	 * @param $orderId
	 * @param $comment
	 * @param $image
	 * @param $video
	 * @param $scoreAttitude
	 * @param $scoreSkill
	 * @param $scoreAppearance
	 * @param $scoreStore
	 * @param $waitingTime
	 * @return mixed
	 * @throws NotFoundException
	 */
	public function orderComment($memberId,$orderId,$comment,$image,$video,$scoreAttitude,$scoreSkill,$scoreAppearance,$scoreStore,$waitingTime)
	{
		$image = empty($image) ? null : json_decode($image,true);
		// 查询该订单的信息是否存在
		$orderInfo = $this->orderRepository->isExisted($orderId);
		if($orderInfo->is_evaluate){
			throw new NotFoundException(['msg'=>"订单已经被评价过了！"]);
		}
		// 查询该订单是不是该用户的
		if ($orderInfo->member_id != $memberId) {
			throw  new NotFoundException(['msg'=>'评论订单不是你自己的！']);
		}
		// 查询该订单是否已经付款完成
		if (!$orderInfo->is_pay && ($orderInfo->status != config('constant.orderStatus')['done'])) {
			throw  new NotFoundException(["msg"=>"该订单还未完成，请完成后评价！"]);
		}
		/**
		 * 评论资源上传处理
		 */
		//开启事务
		DB::beginTransaction();
		$image = empty($image) ? 0 : $this->pictureRepository->insert($image);
		$video = empty($video) ? 0 : $this->videoRepository->insertData($video);
		// 插入评论
		$commentInfo = $this->orderCommentRepository->addComment($orderInfo->id,$orderInfo->barber_id,$orderInfo->store_id,$memberId,$comment,$scoreAttitude,$scoreSkill,$scoreAppearance,$scoreStore,$waitingTime);
		$res = true;
		// 插入评论资源
		if(!empty($image) || !empty($video)){
			$commentResource = $this->commentResourceRepository->insertData($commentInfo->id,$memberId, $image,$video);
			$res = empty($commentResource) ? false : true;
		}
		// 修改订单状态
		$orderInfo->is_evaluate = config('constant.yes');
		$orderInfo->save();
		$res ? DB::commit() : DB::rollBack();
		return $res;
	}

	/**
	 *  订单评论回复
	 * @param $commentId
	 * @param $userId
	 * @param $content
	 * @return mixed
	 * @throws NotFoundException
	 */
	public function orderReply($userId,$commentId,$content)
	{
		$commentInfo = $this->orderCommentRepository->isExisted($commentId);
		return $this->orderCommentReplyRepository->addReply($commentId,$userId,$content);
	}

	/**
	 * 删除评论回复
	 * @param $requestData
	 * @return int
	 */
	public function deleteReply($requestData){
		return $this->orderCommentReplyRepository->del($requestData);
	}

	/**
	 * 评论点赞
	 * @param $memberId
	 * @param $commentId
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 * @author Happy dong
	 * @modifiedTime 2019-07-31 17:36
	 */
	public function addCommentLike($memberId,$commentId){
		$this->orderCommentRepository->isExisted($commentId);
		return $this->orderCommentLikeRepository->add($memberId,$commentId);
	}

	/**
	 * 评论点赞删除
	 * @param $ids
	 * @return int
	 */
	public function delCommentLike($commentId,$memberId){
		return $this->orderCommentLikeRepository->deleteById($commentId,$memberId);
	}

	/**
	 * 评论获取
	 * @param $requestData
	 * @return string
	 */
	public function getOrderComment($requestData){
		return $this->orderCommentRepository->getOrderComment($requestData);
	}

	/**
	 * 删除订单评论
	 * @param $memberId
	 * @param $orderCommentId
	 * @return int
	 * @throws NotFoundException
	 * @throws \Flower\Core\Repository\Exceptions\ValidatorException
	 */
	public function deleteComment($memberId,$orderCommentId){
		$orderComment = $this->orderCommentRepository->isExisted($orderCommentId);
		$orderId = $orderCommentId->order_id;
		if($orderComment->member_id != $memberId){
			throw new NotFoundException(['msg'=>'您对该订单评论无权操作！']);
		}
		$this->orderRepository->updateById($orderId,config('constant.orderStatus.done'),config('constant.yes'),config('constant.no'),null,config('constant.no'));
		return  $this->orderCommentRepository->delete($orderCommentId);
	}


	/**
	 * 获取理发师评论列表
	 * @param $storeId
	 * @param $barberId
	 * @param $orderId
	 * @param $pageSize
	 * @param $memberId
	 * @return array|mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getBarberListComment($storeId,$barberId,$orderId,$pageSize,$memberId){
		$list = $this->orderCommentRepository->getBarberListComment($storeId,$barberId,$orderId,$pageSize,$memberId);
		return $list;
	}

	public function commentViewTimesSetInc($commentId){
		return $this->orderCommentRepository->viewTimesSetInc($commentId);
	}
}